<?php
class EWebby_HttpCookieBag implements EWebby_IPersist {
	private static $_COOKIE_NAME = '__PROFILECOOKIE__';
	public static $timeOut = 90;	// seconds
	
	public static function getInstance() {
		if (isset($_COOKIE [self::$_COOKIE_NAME])) {
			$obj = unserialize( base64_decode($_COOKIE [self::$_COOKIE_NAME]) );
			if ( $obj != null && ($obj instanceof EWebby_HttpCookieBag) ) {				
				return $obj;
			}
		}

		return null;
	}
	
	public function save() {
		Application::$current->cookieBag = $this;
		
		setcookie ( self::$_COOKIE_NAME,
			base64_encode ( serialize ($this) ),
			time () + self::$timeOut );
	}
	
	public function clear() {
		Application::$current->cookieBag = null;
		setcookie ( self::$_COOKIE_NAME );
	}
}